<template>
  <div class="bg">
    <div class="login-container">
      <div class="login-top">
        <img :src="logo" alt="" />
        <span>动力港能源管理平台</span>
      </div>
      <div class="login-bottom">
        <el-form
          ref="ruleFormRef"
          style="max-width: 600px"
          :model="ruleForm"
          status-icon
          :rules="rules"
          label-width="auto"
        >
          <el-form-item prop="username">
            <el-input
              placeholder="请输入用户名"
              v-model="ruleForm.username"
              :prefix-icon="User"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              placeholder="请输入密码"
              v-model="ruleForm.password"
              type="password"
              :prefix-icon="Lock"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="submitForm(ruleFormRef)"
              style="width: 100%"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import logo from "../assets/logo.png";
import { ref, reactive } from "vue";
import { useUserStore } from "@/store/auth";
import { useRouter } from "vue-router";
import { Lock, User } from "@element-plus/icons-vue";
import type { FormInstance, FormRules } from "element-plus";
const userStore = useUserStore();
const router = useRouter();
interface RuleForm {
  username: string;
  password: string;
}
const ruleForm: RuleForm = reactive({
  username: "",
  password: "",
});
const ruleFormRef = ref<FormInstance>();
const rules = reactive<FormRules<RuleForm>>({
  username: [
    { required: true, message: "用户名不能为空", trigger: "blur" },
    { min: 4, max: 8, message: "用户名要求4-8位数字字母组合", trigger: "blur" },
  ],
  password: [{ required: true, message: "密码不能为空", trigger: "blur" }],
});
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (valid) {
      await userStore.login(ruleForm);
      router.push("/");
    } else {
      console.log("账号或密码输入错误，请重新登录!");
    }
  });
};
</script>

<style lang="less" scoped>
.bg {
  background-image: url("../assets/bg.png");
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  height: 100vh;
  .login-container {
    position: absolute;
    top: 50%;
    left: 10%;
    margin-top: -200px;
    width: 500px;
    height: 300px;
    padding: 50px;
    text-align: center;
    box-shadow: 0 0 10px 10px #f4f4f4;

    .login-top {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 50px;
      img {
        width: 70px;
        height: 70px;
      }
      span {
        font-size: 32px;
        font-weight: 600;
        margin-left: 20px;
        color: #0e3594;
      }
    }
    .login-bottom {
      margin-top: 40px;
    }
  }
}
</style>
